#CC=gcc
#CC=clang
ifneq ($(CC),clang)
	CFLAGS=-march=native -Wall
endif

CFLAGS+=-O2 -DUSE_GLIB -D_GNU_SOURCE -D"_LARGE_FILES" -D"_LARGEFILE64_SOURCE" -D"_FILE_OFFSET_BITS=64" -I/usr/include/libxml2
CFLAGS+=`pkg-config --cflags glib-2.0`

#LIBS=-lm -lpthread -lxml2 -lncurses -lrt -lbz2 `pkg-config --libs glib-2.0`
LIBS=-lm -lpthread -lxml2 -lncurses -lrt `pkg-config --libs glib-2.0`

OBJS=mpegts_io.o device.o utils.o bitstream.o protocol.o mpegts.o timers.o xmltv.o win.o record.o

all: tuner_ceton tuner_hdhr tuner_restream tuner_mlb tuner_atsc record mpegts_io hls

win.o: win.c win.h
	$(CC) $(CFLAGS) -DUSE_WWIN -c win.c -o win.o

bitstream.o: bitstream.c bitstream.h
	$(CC) $(CFLAGS) -c bitstream.c -o bitstream.o

mpegts.o: mpegts.c mpegts.h
	$(CC) $(CFLAGS) -c mpegts.c -o mpegts.o

mpegts_io.o: mpegts_io.c mpegts_io.h
	$(CC) $(CFLAGS) -c mpegts_io.c -o mpegts_io.o

xmltv.o: xmltv.c xmltv.h
	$(CC) $(CFLAGS) -c xmltv.c -o xmltv.o 

utils.o: utils.c utils.h
	$(CC) $(CFLAGS) -c utils.c -o utils.o

device.o: device.c device.h
	$(CC) $(CFLAGS) -c utils.c -o utils.o
	$(CC) $(CFLAGS) -c device.c -o device.o

protocol.o: protocol.c protocol.h
	$(CC) $(CFLAGS) -c protocol.c -o protocol.o

bitstream: bitstream.c bitstream.h
	$(CC) $(CFLAGS) -D_MAIN_ bitstream.c -o bitstream $(LIBS)

record: record.c mpegts_io.c
	$(CC) $(CFLAGS)  -D_MAIN_ record.c utils.o timers.o bitstream.o protocol.o mpegts.o mpegts_io.o -o record $(LIBS)

protocol: protocol.c protocol.h
	$(CC) $(CFLAGS) -c timers.c -o timers.o
	$(CC) $(CFLAGS) -c record.c -o record.o
	$(CC) $(CFLAGS) -c utils.c -o utils.o
	$(CC) $(CFLAGS) -D_MAIN_ protocol.c utils.o timers.o record.o -o protocol $(LIBS)

timers: timers.c timers.h
	$(CC) $(CFLAGS) -D_MAIN_ timers.c utils.o -o timers $(LIBS)

mpegts: mpegts.c mpegts.h bitstream.o utils.o
	$(CC) $(CFLAGS) -D_MAIN_ bitstream.o utils.o mpegts.c -o mpegts $(LIBS)

utils: utils.c utils.h
	$(CC) $(CFLAGS) -D_MAIN_ utils.c -o utils $(LIBS)

hls: hls.c hls.h
	$(CC) $(CFLAGS) -Wall -D_MAIN_ hls.c -o hls utils.o $(LIBS) -lcurl -lcrypto

xmltv: xmltv.c xmltv.h
	$(CC) $(CFLAGS) -D_MAIN_ xmltv.c -o xmltv $(LIBS)  -lcurl

mpegts_io: mpegts_io.c mpegts_io.h bitstream.o mpegts.o utils.o
	$(CC) $(CFLAGS)  mpegts_io.c utils.o bitstream.o mpegts.o -D_MAIN_ -o mpegts_io $(LIBS)

tuner_ceton: tuner_ceton.c tuner_ceton.h $(OBJS)
	$(CC) $(CFLAGS) -D_MAIN_ tuner_ceton.c $(OBJS) -o tuner_ceton -lcurl $(LIBS)

tuner_hdhr: tuner_hdhr.c tuner_hdhr.h $(OBJS)
	$(CC) $(CFLAGS)  -D_MAIN_ tuner_hdhr.c $(OBJS) -o tuner_hdhr -lhdhomerun $(LIBS)

tuner_atsc: tuner_atsc.c tuner_atsc.h $(OBJS)
	$(CC) $(CFLAGS)  -D_MAIN_ tuner_atsc.c $(OBJS) -o tuner_atsc $(LIBS)

tuner_restream: tuner_restream.c tuner_restream.h xmltv.o $(OBJS)
	$(CC) $(CFLAGS) -D_MAIN_ tuner_restream.c $(OBJS) $(OBJS2) -o tuner_restream $(LIBS) 

tuner_mlb: tuner_mlb.c tuner_mlb.h hls.c hls.h $(OBJS)
	$(CC) $(CFLAGS) -c hls.c -o hls.o 
	$(CC) $(CFLAGS)  -D_MAIN_ tuner_mlb.c hls.o $(OBJS) -o tuner_mlb $(LIBS) -lcurl -lcrypto

clean:
	rm -f *.o filter mpegts_io bitstream mpegts utils device tuner_ceton \
tuner_hdhr tuner_atsc protocol tuner_restream timers freevo_to_edpvr xmltv \
record tuner_mlb hls
